var lastId;

$(function() {
    initId();
	$("#1_1").focus();
});

/* 删除本行 */
function deleteCurrentRow(obj) {
	if (confirm("确定删除?")) {
		$(obj).parents('tr').remove();
        initId();
	}
}
/* 添加到本行前 */
function addBefore(obj) {
	var tr = $(obj).parents('tr');
	tr.clone().insertBefore(tr);
	tr.prev('tr').find('#pronounTd').html("");
    initId();
}
/* 添加到本行之后 */
function addLater(obj) {
	var tr = $(obj).parents('tr');
	tr.clone().insertAfter(tr);
    tr.next('tr').find('#pronounTd').html("");
    initId();
}

function submitAction(obj, type,sid) {
    if(UTIL.checkButton(obj)) {
        var errMsg = checkVocabulary(sid);
        if(errMsg) {
            alert(errMsg);
            UTIL.releaseButton(obj);
        } else {
            if(sid){
                var formData = $('#actionSubmit'+sid).serialize();
				API.analysisSentenceSubdivision(formData, function(res) {
					if (res.code == '0') {
						alert("保存成功");
                        UTIL.releaseButton(obj);
					} else if(res.code == '40044'){
                        alert("存在position2=0的情况生成新的组合");
                        window.location.reload();
                    }else{
						alert(res.desc);
						UTIL.releaseButton(obj);
					}
				});
            }else{
                var formData = $('#actionSubmit').serialize();
                if(type == 1) {
                    API.analysisSentenceEdit(formData, function(res) {
                        if (res.code == '0') {
                            window.location.href = "/analysis/sentence_list.htm";
                            return true;
                        }else if(res.code == '40044'){
                            alert("存在position2=0的情况生成新的组合");
                            window.location.reload();
                        }else if(res.code == '40045'){
                            window.location.href = "/mark_sentence/sentence_list.htm?tabid=41_1";
                        }else if(res.code == '40046'){
                            window.location.href = "/mark_sentence/sentence_list.htm?tabid=41_2";
                        }else{
                            alert(res.desc);
                            UTIL.releaseButton(obj);
                        }
                    });
                } else {
                    API.analysisRuleEdit(formData, function(res) {
                        if (res.code == '0') {
                            window.location.href = "/analysis/rule_list.htm";
                            if(res.desc && res.desc!='成功') {
                                alert(res.desc);
                            }
                            UTIL.releaseButton(obj);
                            return true;
                        } else {
                            alert(res.desc);
                            UTIL.releaseButton(obj);
                        }
                    });
                }
            }
        }
        return false;
	}
}

function checkVocabulary(sid) {
	var errMsg = "";
	var vs = $("#actionSubmit"+sid+" input[name=vocabulary]");
	var sentence = $("#sentence"+sid).val();
	var vStr = '';
	for(var i=0;i<vs.length;i++) {
		if(!$.trim($(vs[i]).val())) {
			return "分词不要有空值";
		}
		vStr += $.trim($(vs[i]).val());
	}
	if(vStr != sentence) {
		errMsg = "分词和句子不匹配";
	}
	return errMsg;
}

// 预览组词效果
function preview(obj) {
	if(UTIL.checkButton(obj)) {
		var errMsg = checkVocabulary('');
		if(errMsg) {
			alert(errMsg);
		} else {
			var formData = $('#actionSubmit').serialize();
			API.analysisSentencePreview(formData, function(res) {
				if (res.code == '0') {
					var preview = res.data.preview;
					$("#preview span").html(preview);
					$("#preview").show();
					return true;
				} else {
					alert(res.desc);
				}
			});
		}
		UTIL.releaseButton(obj);
		return false;
	}
}

function fastSkip(obj) {
	var code = window.event.keyCode;
	var idArr = $(obj).attr("id").split("_");
	var id1 = parseInt(idArr[0]);
	var id2 = parseInt(idArr[1]);
	// 当焦点在提交按钮上时，向上则表示回到最后一行数据
	if(id1 == 0 && code == 38) {
		$("#" + lastId).focus();
		return;
	}
	
	var next;
	// ctrl向左
	if(code==37) {
		// 禁止下拉框中的值切换
		event.returnValue=false;
		next = $("#" + id1 + "_" + --id2);
	}
	// ctrl向上
	if(code==38) {
		event.returnValue=false;
		next = $("#" + --id1 + "_" + id2);
	}
	// ctrl向右
	if(code==39) {
		event.returnValue=false;
		next = $("#" + id1 + "_" + ++id2);
	}
	// ctrl向下
	if(code==40) {
		event.returnValue=false;
		next = $("#" + ++id1 + "_" + id2);
	}

	if(next) {
		if(next.attr("id")) {
			next.focus();
		} else if(code==40) {
			$("#0_0").focus();
		}
	}
}

function initId() {
	$("form").each(function(){
        var typeArray = new Array('input','select','select','select','select','a','a','a');
        var nameArray = new Array('vocabulary','character','property','position1','position2','icoarrowup','icoarrowdown','icodel');
        for(var i=0; i<nameArray.length; i++) {
            var index = 0;
            $(typeArray[i] + "[name='" + nameArray[i] + "']").each(function() {
                $(this).attr("id", ++index + "_" + (i + 1));
            });
        }

        lastId = index + "_1";
    });
}

function changeSource(index) {
	var preId = index + 1;
	while($("#" + preId + "_9").val() != undefined) {
		$("#" + preId + "_9").val($("#" + index + "_9").val());
		preId ++;
	}
}


function getPropnounList(obj,vocabularyNow,propertyNow,index) {
    // 获取所有的词语
    var table = $(obj).parent().parent().parent();
    var vocabulary = [];
    var property = [];

    table.find("input[name='vocabulary']").each(function(){
    	if($(this).val() != vocabularyNow){
            vocabulary.push($(this).val());
        }
	});


    table.find("select[name='property']").find("option:selected").each(function(){
        property.push($(this).val());
        if($(this).val() != propertyNow){
        }
    });
	var td = $(obj).parent().parent().find("#pronounTd");
	td.html('');
	var appendStr = "";
	for(var i=0;i<vocabulary.length;i++){
		appendStr = appendStr +"<div><input type='checkbox' onclick='checkPro(this)' style='vertical-align: middle;' value ='0'>" +
			"<input type='hidden' name = 'pronoun_"+index+"'  value='0' />" +vocabulary[i]+","+property[i]+"</div>";
	}
	td.append(appendStr);

}


function checkPro(obj) {
    var val = $(obj).next().val();
    if(val == '1'){
        $(obj).next().val("0");
    }else{
        $(obj).next().val("1");
    }
	
}